import java.util.function.Consumer;

/**
 * lambda表达式，FunctionalInterface限制接口内只有一个抽象方法
 */
@FunctionalInterface
interface InputPersonInfo {
    Person input();
}

/**
 * 创建出行人信息字符串
 */
@FunctionalInterface
interface PrintPersonInfo {
    String generate(Person person);
}


// 每个枚举值都是 public static final
public enum PersonType {
    成人(1, (() -> {
                Person person = new Person();
                inputBase(person);
                person.setPrice(2000);
                return person;
            }),
            (person -> {
                return String.format(
                        "姓名：%s，年龄：%d，本次出行金额：%f元",
                        person.getName(),
                        person.getAge(),
                        person.getPrice()
                );
            })
    ),
    儿童(2, (() -> {
                Person person = new Person();
                inputBase(person);
                if (person.getAge() > 18)
                    return null;

                System.out.print("是否占床(Y/N): ");
                person.setBed(Menu.INPUT.next().equals("y"));
                person.setPrice(person.isBed() ? 30 : 0);
                return person;
            }),
            (person -> {
                return String.format(
                        "姓名：%s，年龄：%d %s 1.2m以下儿童免费，本次出行金额：%f元",
                        person.getName(),
                        person.getAge(),
                        person.isBed() ? "占床" : "",
                        person.getPrice()
                );
            })
    ),
    老人(3, () -> {
                Person person = new Person();
                inputBase(person);

                if (person.getAge() < 65)
                    return null;

                person.setPrice(1000);
                return person;
            },
            (person) -> {
                return String.format("姓名：%s，年龄：%d 65岁以上老人半价，本次出行金额：%f元",
                        person.getName(),
                        person.getAge(),
                        person.getPrice()
                );
            }
    );


    /**
     * 输入基本信息
     * @return
     */
    private static void inputBase(Person person) {
        System.out.print("请输入姓名: ");
        person.setName(Menu.INPUT.next());
        System.out.print("请输入年龄: ");
        person.setAge(Menu.INPUT.hasNextInt() ? Menu.INPUT.nextInt() : 18);
    }


    private int number;
    private InputPersonInfo inputPersonInfo;
    private PrintPersonInfo printPersonInfo;

    PersonType(int number, InputPersonInfo inputPersonInfo, PrintPersonInfo printPersonInfo) {
        this.number = number;
        this.inputPersonInfo = inputPersonInfo;
        this.printPersonInfo = printPersonInfo;
    }

    public int getNumber() {
        return number;
    }

    public InputPersonInfo getInputPersonInfo() {
        return inputPersonInfo;
    }

    public PrintPersonInfo getPrintPersonInfo() {
        return printPersonInfo;
    }

    public static PersonType valueOf(int number) {
        for (PersonType value : PersonType.values()) {
            if (value.number == number) {
                return value;
            }
        }
        return PersonType.成人;
    }
}
